/**
 * 应用模块以及依赖关系
 */
var myApp = angular.module('myApp', ['ngRoute']);

/**
 * 配置常量
 */
myApp.constant('API', 'http://api.1999.me');
myApp.constant('URL', 'http://test.1999.me');

/**
 * 定义路由
 */
myApp.config(function($routeProvider, $httpProvider, $locationProvider) {
  $routeProvider
    .when('/login', {
      controller: 'loginController',
      templateUrl: 'views/login.html',
      requireLogin: false
    })
    .when('/authorize', {
      controller: 'authorizeController',
      templateUrl: 'views/authorize-message.html',
      requireLogin: false
    })
    .when('/reset', {
      controller: 'resetController',
      templateUrl: 'views/reset.html',
      requireLogin: false
    })
    .when('/apply', {
      controller: 'applyController',
      templateUrl: 'views/apply.html',
      requireLogin: false
    })
    .when('/apply/success', {
      templateUrl: 'views/apply-success.html',
      requireLogin: false
    })
    .when('/wechat', {
      controller: 'wechatController',
      templateUrl: 'views/authorize-message.html',
    })
    .when('/', {
      controller: 'indexController',
      templateUrl: 'views/index.html'
    })
    .when('/item/:id/page', {
      controller: 'itemPageController',
      templateUrl: 'views/item-page.html'
    })
    .when('/item/:id/image', {
      controller: 'itemImageController',
      templateUrl: 'views/item-image.html'
    })
    .when('/item/:id/video', {
      controller: 'itemVideoController',
      templateUrl: 'views/item-video.html'
    })
    .when('/item/:id/demo', {
      controller: 'itemDemoController',
      templateUrl: 'views/item-demo.html'
    })
    .otherwise({
      redirectTo: '/'
    });

    $httpProvider.interceptors.push(function($location, tokenManager, API) {
      return {
        'request': function(request) {
          if ('/' === request.url.substring(0, 1)) {
            var token = tokenManager.getToken();
            if (token) {
              request.url += '?'+$.param({access_token: token.access_token});
            }
            request.url = API.concat(request.url);
          }
          if (angular.isDefined(request.data)) {
            request.headers['Content-Type'] = 'application/x-www-form-urlencoded;';
            request.transformRequest = function(obj) {
              return $.param(obj);
            }
          }
          return request;
        },
        'response': function(response) {
          if (angular.isObject(response.data)) {
            if (response.data.code == 400) {
              tokenManager.clearToken();
              $location.path('/login');
            }
          }
          return response;
        }
      };
    });

  $locationProvider.html5Mode({
    enabled: true,
    requireBase: false
  });
});

/**
 * 访问权限
 */
myApp.run(function($rootScope, $sce, $location, tokenManager) {

  $rootScope.resourceUrl = function(url) {
    return $sce.trustAsResourceUrl(url);
  }

  $rootScope.$on('$routeChangeStart', function(event, next, current) {
    if ((undefined === next.requireLogin) || (false !== next.requireLogin)) {
      var token = tokenManager.getToken();
      if (!token) {
        $location.path('/login');
      }
    }
  });
});
